package de.lehmann.sudoku.userInterface.graphical;

import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.CardLayout;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class STile extends JPanel {;
	private static final long serialVersionUID = -7713868965030176746L;
	private static Font bigFont = new Font("Tahoma", Font.BOLD, 18);
	private static Font smallFont = new Font("Tahoma", Font.PLAIN, 6);
	
	/**
	 * The <b>X</b>-coordinate of this STile.
	 */
	public final int X;
	/**
	 * The <b>Y</b>-coordinate of this STile.
	 */
	public final int Y;
	private JLabel value;
	private JPanel tagContainer;
	private JLabel[] tags;

	/**
	 * Create an STile, that will have its <b>X</b>-coordinate set to <b>xPos</b> <br>
	 * and its <b>Y</b>-coordinate set to <b>yPos</b>.
	 * 
	 * @param xPos
	 * @param yPos
	 */
	protected STile(int xPos, int yPos) {
		X = xPos;
		Y = yPos;
		setBackground(Color.WHITE);
		setLayout(new CardLayout());
		
		value = new JLabel("");
		value.setHorizontalAlignment(SwingConstants.CENTER);
		value.setFont(bigFont);
		add(value, "value");
		
		tagContainer = new JPanel();
		tagContainer.setForeground(Color.DARK_GRAY);
		tagContainer.setLayout(new GridLayout(0, SGrid.active().WIDTH, 0, 0));
		add(tagContainer, "tags");
		
		tags = new JLabel[SGrid.active().SIZE];
		for(int i = 0; i < tags.length; i++){
			tags[i] = new JLabel(Integer.toString(i+1));
			tags[i].setHorizontalAlignment(SwingConstants.CENTER);
			tags[i].setFont(smallFont);
			tagContainer.add(tags[i]);
		}

	}
	
	/**
	 * Sets the font sizes of this STile to match STile.<b>bigFont</b> and STile.<b>smallFont</b>.
	 */
	protected void adaptFont() {
		value.setFont(bigFont);
		for(int i = tags.length-1; i>=0; i--) {
			tags[i].setFont(smallFont);
		}
	}
	
	/**
	 * Sets STile.<b>bigFont</b> and STile.<b>smallFont</b> according to the given tile-size.
	 * @param size - The font size all STiles should use.
	 */
	protected static void setFont(int size) {
		if(size<6){
			size = 6;
		}
		int smallSize = size/3;
		bigFont = new Font("Tahoma", Font.BOLD, size);
		smallFont = new Font("Tahoma", Font.PLAIN, smallSize);
	}
}
